package com.trussell.genesis.windowutils.toolbars;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

import com.trussell.genesis.GenesisImplementor;
import com.trussell.genesis.GenesisImplementor.TerrainEditMode;

/**
 * This class contains the tools to manipulate terrain. This includes the raise,
 * lower, smooth, and noise tools. This is a floatable toolbar that is activated
 * and deactivated when an artist clicks changes the mode of Genesis to or from
 * the "Tile Edit" mode.
 * 
 * @author Tyler Trussell
 */
public class TerrainEditToolBar extends JToolBar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static TerrainEditToolBar instance;

	public static TerrainEditToolBar getInstance() {
		if (instance == null)
			instance = new TerrainEditToolBar();
		return instance;
	}

	JSlider radius;
	JSlider intensity;

	/**
	 * Constructs the toolbar.
	 */
	private TerrainEditToolBar() {
		super("Terrain Edits");
		setFloatable(false);

		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		ButtonGroup bg = new ButtonGroup();

		JToggleButton raise = new JToggleButton();
		JToggleButton lower = new JToggleButton();
		JToggleButton smooth = new JToggleButton();
		JToggleButton noise = new JToggleButton();
		JToggleButton texture = new JToggleButton();
		JToggleButton erase = new JToggleButton();
		JToggleButton place = new JToggleButton();
		JToggleButton edit = new JToggleButton();

		radius = new JSlider();
		intensity = new JSlider();

		intensity.setMinimum(1);
		intensity.setMaximum(10);
		radius.setMinimum(1);
		radius.setMaximum(10);
		intensity.setMaximumSize(new Dimension(100,20));
		radius.setMaximumSize(new Dimension(100,20));

		bg.add(raise);
		bg.add(lower);
		bg.add(smooth);
		bg.add(noise);
		bg.add(texture);
		bg.add(erase);
		bg.add(place);
		bg.add(edit);

		raise.setIcon(new ImageIcon("images/raise-ground.png"));
		lower.setIcon(new ImageIcon("images/lower-ground.png"));
		smooth.setIcon(new ImageIcon("images/smooth-ground.png"));
		noise.setIcon(new ImageIcon("images/make-noise.png"));
		texture.setIcon(new ImageIcon("images/texturebutton.png"));
		erase.setIcon(new ImageIcon("images/erase.png"));
		place.setIcon(new ImageIcon("images/objectplacement.png"));
		edit.setIcon(new ImageIcon("images/editobject.png"));

		raise.setToolTipText("Raise Ground");
		lower.setToolTipText("Lower Ground");
		smooth.setToolTipText("Smooth Ground");
		noise.setToolTipText("Noise Tool");
		texture.setToolTipText("Paint Texture");
		erase.setToolTipText("Erase Texture");
		place.setToolTipText("Place GameObject");
		edit.setToolTipText("Edit GameObject");

		raise.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GenesisImplementor.getInstance().setTerrainEditMode(
						TerrainEditMode.RAISE);
			}
		});

		lower.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GenesisImplementor.getInstance().setTerrainEditMode(
						TerrainEditMode.LOWER);
			}
		});
		
		smooth.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GenesisImplementor.getInstance().setTerrainEditMode(
						TerrainEditMode.SMOOTH);
			}
		});

		add(raise);
		add(lower);
		add(smooth);
		add(noise);
		addSeparator();
		add(texture);
		add(erase);
		addSeparator();
		add(place);
		add(edit);
		addSeparator();
		add(new JLabel("Intensity:"));
		add(intensity);
		add(new JLabel("Radius:"));
		add(radius);

		raise.setSelected(true);
		GenesisImplementor.getInstance().setTerrainEditMode(
				TerrainEditMode.RAISE);

		setVisible(true);
	}

	public int getRadius() {
		return radius.getValue();
	}

	public int getIntensity() {
		return intensity.getValue();
	}
}
